package cn.lhn.ridersclub.controller;

import cn.lhn.ridersclub.anno.TokenVerify;
import cn.lhn.ridersclub.dto.*;
import cn.lhn.ridersclub.entity.Comment;
import cn.lhn.ridersclub.service.CommentService;
import cn.lhn.ridersclub.service.PaperService;
import cn.lhn.ridersclub.service.SupportService;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/paper")
public class PaperController {

    private final PaperService paperService;

    private final SupportService supportService;

    private final CommentService commentService;

    public PaperController(PaperService paperService, SupportService supportService, CommentService commentService) {
        this.paperService = paperService;
        this.supportService = supportService;
        this.commentService = commentService;
    }

    @GetMapping("/getById")
    public Result<PaperInfoDto> getPaperById(Long paperId) {
        PaperInfoDto paperInfoDto = paperService.getPaperById(paperId);
        return Result.success("查询成功", paperInfoDto);
    }

    @GetMapping("/search")
    public Result<List<PaperCardDto>> getPaperByKeyword(String keyword, Long startId) {
        List<PaperCardDto> list = paperService.getByKeyword(keyword, startId);
        return Result.success("查询成功", list);
    }

    @GetMapping("/get")
    public Result<List<PaperCardDto>> getPapers(Long accountId, Long startId) {
        List<PaperCardDto> list = paperService.getPapers(accountId, startId);
        return Result.success("查询成功", list);
    }

    @GetMapping("/getByClubId")
    public Result<List<PaperCardDto>> getPapersByClubId(Long clubId, Long startId) {
        List<PaperCardDto> list = paperService.getPapersByClubId(clubId, startId);
        return Result.success("查询成功", list);
    }

    @GetMapping("/all")
    public Result<List<PaperCardDto>> getPapers() {
        List<PaperCardDto> list = paperService.getPapers();
        return Result.success("查询成功", list);
    }

    @PostMapping("/add")
    @TokenVerify
    public Result<?> addPaper(String title, String connect, Long clubId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        paperService.addPaper(title, connect, clubId, Long.parseLong(accountId));
        FlushController.paperFlag = true;
        return Result.success().message("添加成功");
    }

    @PostMapping("/delete")
    @TokenVerify
    public Result<?> deletePaper(Long paperId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        paperService.deletePaper(paperId, Long.parseLong(accountId));
        FlushController.paperFlag = true;
        return Result.success().message("删除成功");
    }

    @GetMapping("/isSupport")
    @TokenVerify
    public Result<Boolean> isSupport(Long paperId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        Boolean support = supportService.isSupport(paperId, Long.parseLong(accountId));
        return Result.success("查询成功", support);
    }

    @GetMapping("/supportCount")
    public Result<Long> getSupportCount(Long paperId) {
        Long count = supportService.getSupportCount(paperId);
        return Result.success("查询成功", count);
    }

    @PostMapping("/addSupport")
    @TokenVerify
    public Result<?> addSupport(Long paperId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        supportService.addSupport(paperId, Long.parseLong(accountId));
        return Result.success().message("添加成功");
    }

    @PostMapping("/deleteSupport")
    @TokenVerify
    public Result<?> deleteSupport(Long paperId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        supportService.deleteSupport(paperId, Long.parseLong(accountId));
        return Result.success().message("删除成功");
    }

    @GetMapping("/listComment")
    public Result<List<CommentDto>> getCommentsByPaperAndAccount(Long paperId, Long startId) {
        List<CommentDto> list = commentService.listComment(paperId, startId);
        return Result.success("查询成功", list);
    }

    @GetMapping("/getCommentByPaper")
    public Result<List<CommentDto>> getCommentsByPaper(Long paperId, Long startId) {
        List<CommentDto> list = commentService.listComment(paperId, startId);
        return Result.success("查询成功", list);
    }

    @PostMapping("addComment")
    @TokenVerify
    public Result<?> addComment(String content, Long paperId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        commentService.addComment(content, paperId, Long.parseLong(accountId));
        return Result.success().message("添加成功");
    }

    @PostMapping("deleteComment")
    @TokenVerify
    public Result<?> deleteComment(Long commentId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        commentService.deleteComment(commentId, Long.parseLong(accountId));
        return Result.success().message("删除成功");
    }

    @GetMapping("/commentCount")
    public Result<Long> getCommentCount(Long paperId) {
        Long count = commentService.getCommentCount(paperId);
        return Result.success("查询成功", count);
    }

    @PostMapping("/supportComment")
    @TokenVerify
    public Result<?> supportComment(Long commentId, HttpServletRequest request) {
        long accountId = Long.parseLong(request.getHeader("arg0"));
        commentService.supportComment(commentId, accountId);
        return Result.success();
    }

    @PostMapping("/deleteSupportComment")
    @TokenVerify
    public Result<?> deleteSupportComment(Long commentId, HttpServletRequest request) {
        long accountId = Long.parseLong(request.getHeader("arg0"));
        System.out.println("accountId: " + accountId + ", commentId: " + commentId);
        commentService.deleteSupportComment(commentId, accountId);
        return Result.success();
    }

    @GetMapping("/isSupportComment")
    @TokenVerify
    public Result<Boolean> isSupportComment(Long commentId, HttpServletRequest request) {
        long accountId = Long.parseLong(request.getHeader("arg0"));
        return Result.success(commentService.isSupportComment(commentId, accountId));
    }

    @GetMapping("/getLastPaper")
    public Result<PaperCardDto> getLastPaper(Long accountId) {
        PaperCardDto dto = paperService.getLastPaper(accountId);
        return Result.success(dto);
    }

    @GetMapping("/getLastCommentForPaper")
    public Result<CommentDto> getLastCommentForPaper(Long accountId, Long paperId) {
        CommentDto dto = paperService.getLastCommentForPaper(accountId, paperId);
        return Result.success(dto);
    }

    @PostMapping("/addHistory")
    @TokenVerify
    public Result<?> addHistory(Long paperId, HttpServletRequest request) {
        paperService.addHistory(paperId, Long.valueOf(request.getHeader("arg0")));
        return Result.success();
    }

    @GetMapping("/listHistory")
    @TokenVerify
    public Result<List<HistoryDto>> listHistory(Long startId, HttpServletRequest request) {
        List<HistoryDto> list = paperService.listHistory(Long.valueOf(request.getHeader("arg0")), startId);
        return Result.success(list);
    }
}
